package com.jz.storeroomclass.service.impl;

import com.jz.reservoirattributesclass.model.ReservoirattributesVo;
import com.jz.stockidentificationclass.model.StockidentificationVo;
import com.jz.storeroomclass.mapper.StoreroomMapper;
import com.jz.storeroomclass.model.*;
import com.jz.storeroomclass.service.StoreroomService;
import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by WISE on 2021/12/26.
 */
@Service
public class StoreroomServiceImpl implements StoreroomService {

    @Autowired
    private StoreroomMapper storeroomMapper;

    @Override
    public ResultVo firstPage(StoreroomSelectVo storeroomSelectVo) {
        List<StoreroomVo> storeroomVoList = storeroomMapper.queryData(storeroomSelectVo);
        //查询数量
        for (StoreroomVo storeroomVo : storeroomVoList) {

            int status = storeroomVo.getStatus() ? 1 : 0;
            storeroomVo.setStatusName(SubStatusEnum.getNameByStatus(status));
        }
        int count = storeroomMapper.queryCount(storeroomSelectVo);

        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(storeroomVoList);
        allRecordsVo.setTotalSize(count);
        allRecordsVo.setPageIndex(storeroomSelectVo.getPageIndex());
        allRecordsVo.setPageSize(storeroomSelectVo.getPageSize());
        allRecordsVo.countPage(count);

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo addpartClass(StoreroomAddVo partAddVo) {
        //验证编码是否存在
        StoreroomVo storeroomVO = storeroomMapper.queryByCode(partAddVo.getCode());
        if (storeroomVO != null) {
            return ResultBuildVo.error("备件编码已经存在！");
        }
        //验证名称是否存在
        StoreroomVo subClassName = storeroomMapper.queryByName(partAddVo.getName());
        if (subClassName != null) {
            return ResultBuildVo.error("备件名称已经存在！");
        }
        Date nowDate = new Date();
        StoreroomVo classVo = new StoreroomVo();
        BeanUtils.copyProperties(partAddVo, classVo);
        classVo.setCreateTime(nowDate);
        classVo.setCreateName("通过Session拿到当前用户名称");
        storeroomMapper.add(classVo);
        return ResultBuildVo.success();
    }

//
//    @Override
//    public ResultVo queryById(Integer id) {
//
//        CatVo partVo = storeroomMapper.queryById(id);
//
//        return ResultBuildVo.success(partVo);
//    }
//
    @Override
    public ResultVo updateSubClass(@Valid StoreroomUpdateVo partUpdateVo) {
        List<StoreroomVo> storeroomVo = storeroomMapper.queryById(partUpdateVo.getId());
        if (storeroomVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        StoreroomVo subClassName = storeroomMapper.queryByName(partUpdateVo.getName());
        if (subClassName != null && partUpdateVo.getId() != subClassName.getId()) {

            return ResultBuildVo.error("修改名称已存在");
        }

        Date nowDate = new Date();
        StoreroomVo classVo = new StoreroomVo();
        BeanUtils.copyProperties(partUpdateVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
         storeroomMapper.update(classVo);
//        if (version == 0){
//
//            return ResultBuildVo.error("已被修改，刷新重试");
//        }
        return ResultBuildVo.success();

    }

    @Override
    public ResultVo updateStatus(@Valid StoreroomUpdateStatusVo storeroomUpdateStatusVo) {
        List<StoreroomVo> storeroomVo = storeroomMapper.queryById(storeroomUpdateStatusVo.getId());
        if (storeroomVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        Date nowDate = new Date();
        StoreroomVo classVo = new StoreroomVo();
        BeanUtils.copyProperties(storeroomUpdateStatusVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        storeroomMapper.updateStatus(classVo);

        return ResultBuildVo.success();

    }

    @Override
    public ResultVo queryWarehouseTagList() {

      List<StockidentificationVo> stockidentificationVoList = storeroomMapper.queryWarehouseTagList();

        return ResultBuildVo.success(stockidentificationVoList);

    }

    @Override
    public ResultVo queryBusinessAttribute() {

        List<ReservoirattributesVo> reservoirattributesVoList = storeroomMapper.queryBusinessAttribute();

        return ResultBuildVo.success(reservoirattributesVoList);
    }

    @Override
    public ResultVo queryWareHouse() {

        List<StoreroomVo> storeroomVoList = storeroomMapper.queryWareHouse();

        return ResultBuildVo.success(storeroomVoList);
    }

    @Override
    public ResultVo area() {
       List<Address> addressList = storeroomMapper.area();
        return ResultBuildVo.success(addressList);
    }

    @Override
    public ResultVo nation(Integer id) {
       List<Address> addressList = storeroomMapper.nation(id);
        return ResultBuildVo.success(addressList);
    }

    @Override
    public ResultVo province(Integer id) {
        List<Address> addressList = storeroomMapper.province(id);
        return ResultBuildVo.success(addressList);
    }

    @Override
    public ResultVo city(Integer id) {
        List<Address> addressList = storeroomMapper.city(id);
        return ResultBuildVo.success(addressList);
    }

    @Override
    public ResultVo county(Integer id) {

        List<Address> addressList = storeroomMapper.county(id);
        return ResultBuildVo.success(addressList);
    }

}

